package libraryManagerPanels;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.border.LineBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.JButton;

import librarianPanels.WatchReaderCardPanel;

import clientGUI.MainGUI;

public class freezeSubscriptionPanel extends JPanel{
	/**
	 * 
	 */
	static WatchReaderCardPanel wrc;
	static JButton btnFreezeAccount;
	private static final long serialVersionUID = 1L;
	
	/**
	 * 
	 */
	public freezeSubscriptionPanel() {
		setPreferredSize(new Dimension(644, 462));
		setBorder(new LineBorder(new Color(0, 0, 0), 3, true));
		setLayout(null);
		wrc=new WatchReaderCardPanel();
		wrc.setLocation(0, 0);
		
		wrc.remove(wrc.background);
		
		btnFreezeAccount = new JButton("freeze account");
		btnFreezeAccount.setBounds(60, 150, 142, 30);
		btnFreezeAccount.setEnabled(false);
		add(btnFreezeAccount);
		add(wrc);
		btnFreezeAccount.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				String[] s=new String[2];
				s[0]="Freeze";
				s[1]=wrc.textFieldSubsciber.getText();

				if(MainGUI.isMsgEmpty(s))
				{
					if(!MainGUI.testmode)
						JOptionPane.showMessageDialog(null,"the field is empty!");
					return;
				}
				if(!MainGUI.isNumber(s[1]))
				{
					if(!MainGUI.testmode)
						JOptionPane.showMessageDialog(null,"Only Digits");
					wrc.lastId=new String();
					return;
				}
				if(!MainGUI.testmode)
				{
					int n = JOptionPane.showConfirmDialog(new JFrame(),	"Are you sure you want change subscriber status?","An Inane Question",JOptionPane.YES_NO_OPTION);		
					if(n==1)		
						return;
				}
				MainGUI.OBLClient.handleMessageFromClient(s);				
				s[0]="WatchReaderCard";
				s[1]=wrc.lastId;			
				MainGUI.OBLClient.handleMessageFromClient(s);
			}
		});	
		wrc.tabbedPane.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				JTabbedPane sourceTabbedPane = (JTabbedPane) e.getSource();
				if(sourceTabbedPane.getTitleAt(sourceTabbedPane.getSelectedIndex()).contentEquals("Subscriber Details"))
					btnFreezeAccount.repaint();
			}
		});
		wrc.add(wrc.background);
	}

	/**
	 * get a string, and opens an option pane showing a suitable message.
	 * @param info
	 */
	public static void ShowMsgFromServer(String info)
	{
		String msg;
		if(info.compareTo("1")==0)
		{
			msg=new String("Subscribtion status changed successfully!");
		}
		else msg=new String("Could not change subscription status!");
		JOptionPane.showMessageDialog(null,msg);
	}
	
	/**
	 * changes the "freeze" button to "UnFreeze" if the user status i frozen, or change it back to freeze when status is active. 
	 * disables the button otherwise.  
	 * @param status
	 */
	public static void changeButton(String status)
	{
		if(btnFreezeAccount!=null)
		{
			if(status.equals("2")   ||   status.equals("Frozen"))
			{
				btnFreezeAccount.setEnabled(true);
				btnFreezeAccount.setText("Unfreeze account");
			}
			else if(status.equals("1")  ||  status.equals("Active"))
			{
				btnFreezeAccount.setEnabled(true);
				btnFreezeAccount.setText("freeze account");
			}
			else 
				btnFreezeAccount.setEnabled(false);
		}
	}
	

	
}
